/*
 * OrderRepository.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.onesalt.domain.Order;

/**
 * 
 *
 * @author kiri
 * @date 2015年4月19日
 * @version 1.0
 */
public interface OrderRepository extends PagingAndSortingRepository<Order, Long>, JpaSpecificationExecutor<Order> {

	@Query("select o from t_order o, t_dinner d where o.dinnerId = d.dinnerId and o.dinerId = ?1 and d.startTime < ?2")
	public List<Order> findLastOrdersByDinerId(long dinerId, Date startTime);

	public List<Order> findByDinnerIdOrderByCreateTimeDesc(long dinnerId);

	@Query("select count(o.orderId) from t_order o where o.dinnerId = ?1 and o.status in (1, 2)")
	public int getCountByDinnerId(long dinnerId);

	@Query("select o from t_order o, t_dinner d where o.dinnerId = d.dinnerId and o.dinerId = ?1 and d.startTime >= ?2 and d.startTime < ?3 order by d.startTime asc")
	public List<Order> findByDinerIdAndDinnerStartTimeSpan(long dinerId, Date startTimeFrom, Date startTimeTo);

}
